Package MusicLandscape.util
Class ConsoleFieldScanner<T>
- java.lang.Object
-
- MusicLandscape.util.ConsoleFieldScanner<T>
-
- Type Parameters:
T- The type of the value being read
public class ConsoleFieldScanner<T> extends java.lang.ObjectGeneric console function for getting a new value for an entity field from the user.Example:
Integer num = new ConsoleFieldScanner(Integer::parseInt, (Integer i) -> i > 0, null) .scan("Give me a number")Produces this output:Give me a number: <user inputs 123 and presses Enter>
Leading to the variable num having the value 123.- Since:
- ExerciseSheet04
- Version:
- 1
- Author:
- Jonas Altrock (ew20b126@technikum-wien.at)
-
-
Field Summary
Fields Modifier and Type Field Description static java.util.ScannerdefaultScannerThe default input scanner.static booleanNOT_SKIPPABLEconstant to allow readable indication of a non-skippable inputjava.util.ScannerscannerThe scanner object in use.booleanskippableWhether the input can be skipped (by pressing Enter for example).static booleanSKIPPABLEconstant to allow readable indication of a skippable inputjava.util.function.Function<java.lang.String,T>transformerThe input to value transformer function.java.util.function.Predicate<T>validatorThe input value validator function.
-
Constructor Summary
Constructors Constructor Description ConsoleFieldScanner(java.util.function.Function<java.lang.String,T> transformer, java.util.function.Predicate<T> validator, java.util.Scanner scanner)Create a scanner for a single value.ConsoleFieldScanner(java.util.function.Function<java.lang.String,T> transformer, java.util.function.Predicate<T> validator, java.util.Scanner scanner, boolean skippable)Create a scanner for a single value.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.util.ScannergetDefaultScanner()Initialise the default scanner object.Tscan(java.lang.String message)Prompt the user for an input value.
-
-
-
Field Detail
-
SKIPPABLE
public static final boolean SKIPPABLE
constant to allow readable indication of a skippable input- See Also:
- Constant Field Values
-
NOT_SKIPPABLE
public static final boolean NOT_SKIPPABLE
constant to allow readable indication of a non-skippable input- See Also:
- Constant Field Values
-
defaultScanner
public static java.util.Scanner defaultScanner
The default input scanner. Uses System.in if not set from outside.
-
skippable
public boolean skippable
Whether the input can be skipped (by pressing Enter for example).
-
transformer
public java.util.function.Function<java.lang.String,T> transformer
The input to value transformer function.
-
validator
public java.util.function.Predicate<T> validator
The input value validator function.
-
scanner
public java.util.Scanner scanner
The scanner object in use.
-
-
Constructor Detail
-
ConsoleFieldScanner
public ConsoleFieldScanner(java.util.function.Function<java.lang.String,T> transformer, java.util.function.Predicate<T> validator, java.util.Scanner scanner)
Create a scanner for a single value.- Parameters:
transformer- a function that transforms the input string to a valuevalidator- a function that validates the given valuescanner- optional Scanner to use, pass null to use the default scanner (System.in)
-
ConsoleFieldScanner
public ConsoleFieldScanner(java.util.function.Function<java.lang.String,T> transformer, java.util.function.Predicate<T> validator, java.util.Scanner scanner, boolean skippable)
Create a scanner for a single value.- Parameters:
transformer- a function that transforms the input string to a valuevalidator- a function that validates the given valuescanner- optional Scanner to use, pass null to use the default scanner (System.in)skippable- whether the user is allowed to skip entry by just pressing Enter
-
-
Method Detail
-
getDefaultScanner
protected java.util.Scanner getDefaultScanner()
Initialise the default scanner object.- Returns:
- the default scanner
-
scan
public T scan(java.lang.String message)
Prompt the user for an input value.- Parameters:
message- string to print before prompt input- Returns:
- a value or null, if no value was given
-
-